package com.haixiaoke.saas.enterprise.house.controller;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.constant.PlatformConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.house.domain.HouseLead;
import com.haixiaoke.saas.house.service.IHouseLeadService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 房屋留咨信息Controller
 *
 * @author qixi
 * @date 2025-07-08
 */
@RestController
@RequestMapping("/enterprise/house/lead")
public class EnterpriseHouseLeadController extends BaseController {
    @Autowired
    private IHouseLeadService houseLeadService;

    /**
     * 查询房屋留咨信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:houseLead:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseLead houseLead) {
        startPage();
        houseLead.setTenantType(getLoginUser().getTenantId());
        if (StrUtil.equals(PlatformConstants.PC_BUSINESS, getLoginUser().getPlatform())) {
            houseLead.setTenantIds(getTenantIds());
        }
        if (StrUtil.equals(PlatformConstants.MP_WECHAT_BUSINESS, getLoginUser().getPlatform())) {
            houseLead.setTenantId(getLoginUser().getTenantId());
        }
        List<HouseLead> list = houseLeadService.selectHouseLeadList(houseLead);
        return getDataTable(list);
    }


    /**
     * 获取房屋留咨信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:houseLead:query')")
    @GetMapping(value = "/{leadId}")
    public AjaxResult getInfo(@PathVariable("leadId") Long leadId) {
        return success(houseLeadService.selectHouseLeadByLeadId(leadId));
    }


    /**
     * 修改房屋留咨信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:houseLead:edit')")
    @Log(title = "房屋留咨信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseLead houseLead) {
        houseLead.setTenantId(getLoginUser().getTenantId());
        return toAjax(houseLeadService.updateHouseLead(houseLead));
    }

    /**
     * 完成留资信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:houseLead:finish')")
    @Log(title = "房屋留咨信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/finish")
    public AjaxResult finish(@RequestBody HouseLead houseLead) {
        Long leadId = houseLead.getLeadId();
        HouseLead houseLead1 = houseLeadService.selectHouseLeadByLeadId(leadId);
        if (houseLead1 == null) {
            return AjaxResult.error("留资信息不存在");
        }
        if (!StrUtil.equals(getLoginUser().getTenantId(), houseLead1.getTenantId())) {
            return AjaxResult.error("不能操作其他机构的房屋留咨");
        }

        Long handleStatus = houseLead1.getHandleStatus();
        if (handleStatus != 1) {
           return AjaxResult.warn("状态不正确，无法完成");
        }
        houseLead1.setHandleStatus(2L);
        houseLead1.setRemark(houseLead.getRemark());
        return toAjax(houseLeadService.updateHouseLead(houseLead1));
    }

    /**
     * 删除房屋留咨信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:houseLead:remove')")
    @Log(title = "房屋留咨信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leadId}")
    public AjaxResult remove(@PathVariable Long leadId) {
        return toAjax(houseLeadService.deleteHouseLeadByLeadId(leadId, getLoginUser().getTenantId()));
    }
}
